﻿#if NETFX || NETCORE

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Apewer.Surface
{

    /// <summary></summary>
    public class BlockLabel : Label
    {

        private IntPtr _formptr;
        private bool _movable = false;
        private bool _hover = false;
        private bool _locked = false;
        private bool _dynamic = false;

        private Color _wallnormal = FormsUtility.Transparent;
        private Color _wallhover = FormsUtility.Transparent;
        private Color _walllocked = FormsUtility.Transparent;
        private Color _textnormal = FormsUtility.Black;
        private Color _texthover = FormsUtility.Black;
        private Color _textlocked = FormsUtility.Black;

        /// <summary>界面线程调用器。</summary>
        protected delegate void Invoker();

        private void _init(string text, Form form)
        {
            AutoEllipsis = true;
            AutoSize = false;
            Font = FormsUtility.DefaultFont;
            TextAlign = ContentAlignment.MiddleLeft;
            BackColor = _wallnormal;
            ForeColor = FormsUtility.Black;
            AutoEllipsis = true;

            Text = string.IsNullOrEmpty(text) ? "" : text;

            if (form != null)
            {
                _formptr = form.Handle;
                _movable = true;
                MouseDown += Event_Caption_MouseDown;
            }
        }

        /// <summary></summary>
        public BlockLabel()
        {
            _init("", null);
            this.MouseMove += Event_This_MouseMove;
            this.MouseLeave += Event_This_MouseLeave;
        }

        private void Event_This_MouseLeave(object sender, EventArgs e)
        {
            Hover = false;
        }

        private void Event_This_MouseMove(object sender, MouseEventArgs e)
        {
            Hover = true;
        }

        /// <summary></summary>
        /// <param name="text"></param>
        public BlockLabel(string text)
        {
            _init(text, null);
        }

        /// <summary></summary>
        /// <param name="form"></param>
        public BlockLabel(Form form) => _init("", form);

        /// <summary></summary>
        /// <param name="form"></param>
        /// <param name="text"></param>
        public BlockLabel(Form form, string text) => _init(text, form);

        /// <summary></summary>
        /// <param name="text"></param>
        /// <param name="form"></param>
        public BlockLabel(string text, Form form) => _init(text, form);

        private void Event_Caption_MouseDown(object sender, MouseEventArgs e)
        {
            if (_movable) FormsUtility.MoveForm(_formptr);
        }

        /// <summary></summary>
        public string Caption
        {
            get { return Text; }
            set
            {
                if (this.IsHandleCreated)
                {
                    try
                    {
                        this.Invoke(new Invoker(delegate ()
                        {
                            Text = string.IsNullOrEmpty(value) ? "" : value.Trim();
                        }));
                    }
                    catch { }
                }
                else
                {
                    try
                    {
                        Text = string.IsNullOrEmpty(value) ? "" : value.Trim();
                    }
                    catch { }
                }
            }
        }

        /// <summary></summary>
        public bool Locked
        {
            get { return _locked; }
            set
            {
                var vchanged = (value != _locked);
                _locked = value;
                if (vchanged) UpdateColor();
            }
        }

        private bool Hover
        {
            get { return _hover; }
            set
            {
                var vchanged = (value != _hover);
                _hover = value;
                if (vchanged) UpdateColor();
            }
        }

        private void UpdateColor()
        {
            if (!_dynamic) return;
            if (Locked)
            {
                BackColor = _walllocked;
                ForeColor = _textlocked;
            }
            else if (_hover)
            {
                BackColor = _wallhover;
                ForeColor = _texthover;
            }
            else
            {
                BackColor = _wallnormal;
                ForeColor = _textnormal;
            }
        }

        /// <summary>启用鼠标悬停和锁定样式。</summary>
        public bool DynamicStyle { get { return _dynamic; } set { _dynamic = value; } }

        /// <summary></summary>
        public Color WallNormal { get { return _wallnormal; } set { _wallnormal = value; UpdateColor(); } }

        /// <summary></summary>
        public Color WallHover { get { return _wallhover; } set { _wallhover = value; UpdateColor(); } }

        /// <summary></summary>
        public Color WallLocked { get { return _walllocked; } set { _walllocked = value; UpdateColor(); } }

        /// <summary></summary>
        public Color TextNormal { get { return _textnormal; } set { _textnormal = value; UpdateColor(); } }

        /// <summary></summary>
        public Color TextHover { get { return _texthover; } set { _texthover = value; UpdateColor(); } }

        /// <summary></summary>
        public Color TextLocked { get { return _textlocked; } set { _textlocked = value; UpdateColor(); } }


    }
}

#endif
